using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.UI.YGUI;

namespace com.yoozoo.gta
{
    public class TimelineBgSizeProcessor : UnityEditor.Editor
    {
        [MenuItem("CustomTools/修改Timeline背景图片大小")]
        private static void InspectPrefabYImages()
        {
            GameObject[] selectedPrefabs = Selection.gameObjects;

            foreach (GameObject prefab in selectedPrefabs)
            {
                var canvas = prefab.transform.Find("Canvas");
                if (canvas!=null)
                {
                    var scaler2 = canvas.GetComponent<CanvasScaler>();
                    if (scaler2!=null)
                    {
                        GameObject.DestroyImmediate(scaler2,true);
                    }
                }

                var scaler = canvas.gameObject.AddComponent<CanvasScaler>();
                scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
                scaler.referenceResolution = new Vector2(1334, 750);
                scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;
                YImage[] yImages = prefab.GetComponentsInChildren<YImage>(true);

                foreach (YImage yImage in yImages)
                {
                    yImage.rectTransform.anchoredPosition = Vector2.zero;
                    yImage.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
                    yImage.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
                    yImage.rectTransform.pivot = new Vector2(0.5f, 0.5f);
                    yImage.rectTransform.sizeDelta = new Vector2(1650,1000);
                    // if (yImage.gameObject.GetComponent<AspectRatioFitter>() == null)
                    // {
                    //     var fitter = yImage.gameObject.AddComponent<AspectRatioFitter>();
                    //     fitter.aspectRatio = 2.2f;
                    // }
                }
            }
            AssetDatabase.SaveAssets();
        }
    }
}
